package controllers;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import play.*;
import play.modules.facebook.FbGraph;
import play.modules.facebook.FbGraphException;
import play.mvc.*;

import java.util.*;
import play.data.validation.*;

import models.*;

public class Application extends Controller {

    public static void index() {
        render();
    }
	
	public static void dontBuyThis(@Valid Entry entry){
	
		 if(validation.hasErrors()) {
            render("@index");
        }
		
		render(entry);
	}
	
	
	public static void facebookLogin() {
    try {
        //http://developers.facebook.com/docs/reference/api/user/
        JsonObject profile = FbGraph.getObject("me"); // fetch the logged in user
        String email = profile.get("email").getAsString(); // retrieve the email
        // do useful things
        Scope.Session.current().put("name", profile.get("name"));

        Scope.Session.current().put("email", email); // put the email into the session (for the Secure module)
        JsonArray connection = FbGraph.getConnection("me/friends");
        Scope.Session.current().put("friends", connection.get(0));;
    } catch (FbGraphException fbge) {
        flash.error(fbge.getMessage());
        if (fbge.getType() != null && fbge.getType().equals("OAuthException")) {
            Scope.Session.current().remove("username");
        }
    }
    redirect("/");
}

}